using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Danhsachtour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            LoadData();

        }
    }
    private void LoadData()
    {
        pager.PageSize = 5;
        Criteria cri = new Criteria();
        int total = 0;
        if (txttent != null)
            cri.add(Condition.AND, tbTourCollumn .tentour .ToString(), Condition.LIKE, txttent.Text);

        tbTourCollection  tourCollection = tbTourBUS .Get(cri.Criter, pager.CurrentIndex, pager.PageSize,
       tbTourCollumn.matour .ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = tourCollection;
        Repeater1.DataBind();

        pager.ItemCount = total;

    }

    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = Convert.ToInt32(e.CommandArgument);
        LoadData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            tbTour  tour = (tbTour )e.Item.DataItem;

            HyperLink hp_tent = (HyperLink)e.Item.FindControl("hp_tentour");
            hp_tent.Text = tour.Tentour;
            hp_tent.NavigateUrl = "Chitiettour.aspx?id=" + tour.Matour.ToString();

            Image img_t = (Image)e.Item.FindControl("img_t");
            img_t.ImageUrl = "~/Hinhanh/Hinh/" + tour.Hinh;


            tbLoaitour  ltour= tbLoaitourBUS .GetByID(tour.Maloaitour );
            if (ltour  != null)
            {
                Literal lit_lt= (Literal)e.Item.FindControl("lit_ltour");
                lit_lt.Text = ltour.Tenloaitour ;
            }

            Label lb_pt = (Label)e.Item.FindControl("lit_pt");
            lb_pt.Text = tour.Phuongtien;

            Literal lit_gia = (Literal)e.Item.FindControl("lit_gia");
            lit_gia.Text = tour.Gia .ToString() + " vnd";

           

            Literal lit_kh = (Literal)e.Item.FindControl("lit_kh");
            lit_kh.Text = tour.Khoihanh ;

            HyperLink hp_sua = (HyperLink)e.Item.FindControl("hp_sua");
            hp_sua.NavigateUrl = "~/Admin/Suatour.aspx?id=" + tour.Matour .ToString();

           



        }
    }
    protected void lbt_xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_xoa");
            if (cb_xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbTour  tour = tbTourBUS .GetByID(id);

                if (tour.Hinh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + tour.Hinh));
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" + tour.Hinh));
                }
                tbTourBUS .Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
